package com.qingcheng.config;

import com.alibaba.fastjson.JSON;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.dao.CategoryMapper;
import com.qingcheng.dao.SpecMapper;
import com.qingcheng.pojo.goods.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author:boy
 * @Date:2019/7/12
 * @description:
 * @ModifiedBy:
 */
@Component
public class CategoryAndSpecToRedis {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Scheduled(cron = "0 0 0/23 * * ?")
    public void task(){
        System.out.println("定时更新品牌列表和规格列表");
        List<Category> categories = categoryMapper.selectAll();
        for (Category category : categories) {
            String categoryName = category.getName();
            BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(categoryName);
            List<Map> brandList = brandMapper.findListByCategoryName(categoryName);
            String brand = JSON.toJSONString(brandList);
            boundHashOperations.put("brandList",brand);
            List<Map> specList = specMapper.findListByCategoryName(categoryName);
            String spec = JSON.toJSONString(specList);
            boundHashOperations.put("specList",spec);
        }

    }
}
